package com.ego.item.controller;

import com.ego.common.pojo.PageResult;
import com.ego.item.pojo.Brand;
import com.ego.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Resource
    private BrandService brandService;

    @GetMapping("/page")
    public ResponseEntity<PageResult> queryBrandByPage(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                                       @RequestParam(value = "sortBy",defaultValue = "") String sortBy,
                                                       @RequestParam(value = "descending",defaultValue = "false") Boolean descending,
                                                       @RequestParam(value = "key",required = false)String key){
        PageResult result = brandService.page(pageNo,pageSize,sortBy,descending,key);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> findById(@PathVariable("cid") Integer cid){
        return ResponseEntity.ok(brandService.findById(cid));
    }

    @GetMapping("/list/bids")
    ResponseEntity<List<Brand>> queryListByIdList(@RequestParam("bids") List<Long> bids) {
        return ResponseEntity.ok(brandService.findListByIds(bids));
    }

    @PostMapping
    public ResponseEntity<Void> save(Brand brand,@RequestParam("cids") List<Long> cids){
        brandService.save(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> update(Brand brand,@RequestParam("cids") List<Long> cids){
        brandService.update(brand,cids);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping
    public ResponseEntity<Void> deleteById(@RequestParam("id") Long id){
        brandService.deleteById(id);
        return ResponseEntity.ok().build();
    }
}
